package TeachLesson;

import TeachLesson.CourseWork.CourseWorkDao;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "addCourseWorkServlet")
public class addCourseWorkServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("GBK");
        request.setCharacterEncoding("GBK");

        //新建smartupload对象
        SmartUpload su=new SmartUpload();
        //初始化
        su.initialize(getServletConfig(),request,response);
        //设置应许上传的最大文件
        su.setMaxFileSize(1024*1024*10);
        //准备上传
        try {
            su.upload();
            //获取参数
            //作业标题
            String title=su.getRequest().getParameter("title");
            //作业描述
            String workDesc=su.getRequest().getParameter("workDesc");
            //课程编号
            String courseNo=su.getRequest().getParameter("courseNo");
            //教师工号
            String workerNo=su.getRequest().getParameter("workerNo");
            //截止日期
            String endTime=su.getRequest().getParameter("endTime");
            //文件路径
            String filePath=su.getRequest().getParameter("filePath");
            //等级制度
            String evaluteType=su.getRequest().getParameter("evaluteType");

            //获取上传的文件
            com.jspsmart.upload.File tempFile = su.getFiles().getFile(0);
            String fileName=tempFile.getFileName();



            //执行作业上传
              //将endTime转化为日期类型
            //String endTimeS= new SimpleDateFormat("yyyy-MM-dd HH:mm").format(endTime);
              //执行sql语句
            CourseWorkDao courseWorkDao=new CourseWorkDao();
            String filePathS=filePath+"/"+fileName;
            int row=courseWorkDao.addCourseWork(workerNo,courseNo,endTime,title,workDesc,evaluteType,filePathS);
            if(row>0){
                response.getWriter().println("插入成功");
            }else {
                response.getWriter().println("插入失败");
            }
            int n=su.save(filePath);
            response.sendRedirect("/Teachers/courseWorkMangement.jsp?workerNo="+workerNo+"&courseNo="+courseNo);
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doPost(request,response);
    }
}
